//
//  ForgotPSWViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/23.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "UIButton+CountDown.h"
#import "ForgotPSWViewController.h"
#import "ChangeLoginPSWViewController.h"

@interface ForgotPSWViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;

@property (weak, nonatomic) IBOutlet UITextField *V_CodeTF;
@property (weak, nonatomic) IBOutlet UITextField *pswTF;
@property (weak, nonatomic) IBOutlet UITextField *surePswTF;

@property (weak, nonatomic) IBOutlet UIButton *V_CodeBtn;


@end

@implementation ForgotPSWViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"忘记密码";
    self.phoneNumTF.delegate = self;
    [self.V_CodeBtn pp_clipCorners:UIRectCornerAllCorners radius:6 border:1 color:PP_Default_Color_Red_FF3737];

}

- (IBAction)getVCode:(id)sender {
    
    if ([Utilities checkPhoneTel:self.phoneNumTF.text]) {

        [self request_VCode];
        [self.V_CodeBtn startWithTime:60 title:@"重新获取" countDownTitle:@"s" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];
        [self.V_CodeTF becomeFirstResponder];
        
    } else {
        
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        [self.phoneNumTF becomeFirstResponder];
    }
}

#pragma mark 请求二维码
-(void)request_VCode {
    
    NSDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.phoneNumTF.text forKey:@"phone"];
    [dic setValue:@"2" forKey:@"type"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_REGISTER_SMSCODE];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

- (void)request_ChangePSW {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.phoneNumTF.text forKey:@"phone"];
    [dic setValue:self.V_CodeTF.text forKey:@"smsCode"];
    [dic setValue:self.pswTF.text forKey:@"password"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_LOGIN_FORGOT_PSW];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:59 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * code = [data valueForKey:@"code"];
            
                if ([code isEqualToString:@"200"]) {
                    
                    //如果用户之前保存过密码，就把那个密码置空
                    if ([TTUserModel sharedObject].isSaveAccountAndPassword) {
                        [[TTUserModel sharedObject].userModelDefault setValue:@"" forKey:@"PP_user_password"];
                        [[TTUserModel sharedObject].userModelDefault synchronize];
                    }
                    
                    [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    
                    [weakSelf.view endEditing:YES];
                    [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                    [SVProgressHUD dismissWithDelay:1];
                }

        } else {
            
            [self alert:@"返回为空"];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

- (IBAction)handleRequest:(id)sender {
    
    
    if (!self.phoneNumTF.text.length) {
        
        [self alert:@"请输入手机号"];
    } else {
        
        if (!self.V_CodeTF.text.length) {
            
            [self alert:@"请输入验证码"];
        } else {
            
            if (![self.surePswTF.text isEqualToString:self.pswTF.text]) {
                
                [self alert:@"两次密码输入不一致"];
                
            } else {
                
                [self request_ChangePSW];

            }
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.phoneNumTF) {
        // 这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            
            return YES;
            
        }  else if (self.phoneNumTF.text.length >= 11) {
            
            self.phoneNumTF.text = [textField.text substringToIndex:11];
            
            [SVProgressHUD showInfoWithStatus:@"手机号为11为数字"];
            [SVProgressHUD dismissWithDelay:0.5];
            
            return NO;
        }
    }
    
    return YES;
}


@end
